package com.example.ivos.geofence.service.impl;

import com.example.ivos.base.exception.ServiceException;
import com.example.ivos.base.response.StatusCode;
import com.example.ivos.geofence.mapper.GeofenceMapper;
import com.example.ivos.geofence.pojo.dto.GeofenceQuery;
import com.example.ivos.geofence.pojo.dto.GeofenceSaveParam;
import com.example.ivos.geofence.pojo.entity.Geofence;
import com.example.ivos.geofence.pojo.vo.GeofenceVO;
import com.example.ivos.geofence.service.GeofenceService;
import com.example.ivos.vehicle.mapper.VehicleMapper;
import com.example.ivos.vehicle.pojo.dto.VehicleQuery;
import com.example.ivos.vehicle.pojo.entity.Vehicle;
import com.example.ivos.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    GeofenceMapper geofenceMapper;
    @Autowired
    VehicleMapper vehicleMapper;

    @Override
    public List<GeofenceVO> selectGeofence(GeofenceQuery geofenceQuery) {
        log.debug("查询电子围栏业务参数:{}", geofenceQuery);
        List<GeofenceVO> list = geofenceMapper.selectGeofence(geofenceQuery);
        //需要补全每个围栏的车辆数据
        //1.遍历取出每个围栏VO
        for (int i = 0; i < list.size(); i++) {
            GeofenceVO  geofenceVO = list.get(i);
            //2.准备车辆查询的DTO对象VehicleQuery
            VehicleQuery vehicleQuery = new VehicleQuery();
            vehicleQuery.setGeofenceId(geofenceVO.getId());
            List<VehicleVO> vehicleVOList = vehicleMapper.selectVehicle(vehicleQuery);
            //3.获取当前围栏上绑定的车辆总数
            int totalNum = vehicleVOList.size();
            //4.获取当前围栏上的可用车辆数
            int availableNum = 0;
            for(VehicleVO vehicleVO : vehicleVOList){
                if(vehicleVO.getStatus().equals("1")){
                    availableNum++;
                }
            }
            //5.给当前围栏VO补全车辆数据
            geofenceVO.setTotalNum(totalNum);
            geofenceVO.setAvailableNum(availableNum);
            geofenceVO.setVehicleList(vehicleVOList);
        }
        return list;
    }

    @Override
    public void addGeofence(GeofenceSaveParam geofenceSaveParam) {
        log.debug("添加围栏信息业务:geofenceSaveParam={}",geofenceSaveParam);
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceSaveParam,geofence);
        if (geofenceSaveParam.getId() == null){
            geofence.setStatus("1");
            geofence.setCreateTime(new Date());
            geofenceMapper.insert(geofence);
        }else {
            geofence.setUpdateTime(new Date());
            geofenceMapper.update(geofence);
        }


    }
    @Override
    public void deleteGeofence(Long id) {
        log.debug("删除围栏信息业务:id={}",id);
        //先查围栏绑定的车辆
        VehicleQuery  vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
        List<VehicleVO> vehicleList = vehicleMapper.selectVehicle(vehicleQuery);
        if (vehicleList.size()>0&&vehicleList!=null){
            throw  new ServiceException(StatusCode.VEHICLE_EXISTS);
        }
        else {
        geofenceMapper.delete(id);}
    }
}
